local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script, CommonOHOH.files.BaseComponent());
local CallBackManager = CommonOHOH.load.CallBackManager();
local InstanceManager = CommonOHOH.load.InstanceManager();
local V3tool=CommonOHOH.load.Vector3Tool()
local Mtool=require(script.Parent.Parent.tool)
local V3tool= _G.CommonOHOH.load.Vector3Tool()

---------控制怪物的攻击逻辑---------

local HurtTouchName="hurt"


function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);


    self.lastAttackTime=os.clock() --上次跟新时间错

    self.AttackEvent=self.parent.AttackEvent
    self.ChaseTargetEvent=self.parent.ChaseTargetEvent

    self.Battle=self.parent.Battle

    self.AttackCd=self.parent.attribute.AttackCd
    self.Dmage=self.parent.attribute.Damge

    self.parent:AttributeValueChanged("AttackCd",function()
        self.AttackCd=self.parent.attribute.AttackCd
    end)
    self.parent:AttributeValueChanged("Damge",function()
        self.Dmage=self.parent.attribute.Damge
    end)


    --进入追击状态
    CallBackManager:AddRbxConnect(self,  self.parent.ChaseTargetEvent, function(obj)
        self:CheckAttackUpd()
    end);


end;


function M.dtor(self)
    self:Clear()

	M.super.dtor(self);
end




function M.Clear(self)
    if self.updCon then
        self.updCon:Disconnect()
        self.updCon=nil
    end
end

function M.CheckAttackUpd(self)
    if self.updCon then
            return 
    end

    self.updCon=self:AddStepUpdate(function()
        if os.clock()-self.lastAttackTime>=self.AttackCd then
            local tagPlayer=self:GetAttackPlayer() --寻找一个攻击目标
            if tagPlayer then --存在攻击目标
                self.lastAttackTime=os.clock()
                self:oneAttack(tagPlayer) --对攻击目标发起一次攻击
            end
        end

    end)

end


-- 获取攻击目标
function M.GetAttackPlayer(self)
    if self.LastAttackPlayer and self:IsCanAttack(self.LastAttackPlayer) then --存在旧的攻击且合法，返回
        return  self.LastAttackPlayer
    else-- 返回一个新的攻击目标（可以为空值）
        local new=self:CreateNewAttackPlayer()
        self.LastAttackPlayer=new
        return new
    end
end

function M.CreateNewAttackPlayer(self)
    --寻找一个离自己最近的player
    local player= Mtool.FindClosestDistancePlayer(self.parent,self.Battle:GetAllPlayerList())

    if player and self:IsCanAttack(player) then --可以被攻击，返回
        return player
    end
end 

local R=  CommonOHOH.config.Game.MonsterAttackSqrRange

-- 获取攻击范围的平方值
function M.GetAttackSqrRange(self)
    return R
end

--判定某个player，是否能被攻击
function M.IsCanAttack(self,player)
    if not self.Battle:IsHadPlayerInst(player.main_inst) then
        return false
    end
    if V3tool.SqrMagnitude(player.character:GetPosition(),self.parent:GetPosition())>self:GetAttackSqrRange() then
        return false
    end
    return true
end

-- 产生一次攻击
function M.oneAttack(self,player)
    self.parent:SetLookAt( player.character:GetPosition())

    -- --对玩家产生伤害
    player:GetComponent(CommonOHOH.load.Common_AttributeCtrl()):Hurt(self.Dmage)

    self.AttackEvent:Action(player)
end




return M